/*
	JFrame Login. Es el acceso al sistema que tienen los empleados.
	Aqui se validara que trabajador accede al sistema.
	//1 Funcion del botï¿½n Ingresar. Llama a un metodo para iniciar sesion.
	//2 Funcion del botï¿½n salir. Hace una pregunta de confirmaciï¿½n antes de salir.
	//3 Funcion ActionListener de la barra de progreso. Aqui se verifica que trabajador entro.
	//4 Mï¿½todo que oculta este JFrame. Esto pasa cuando los datos de login fueron correctos.
	//5 Mï¿½todo que activa la barra de progreso. Pasa despues de que los datos fueron ingresados
	 	correctamente y antes de que se oculte el JFrame.
	//6 Mï¿½todo que inicia sesion y valida los datos ingresados.
 */

package com.view;

import com.action.proceso4.LoginAction;
import com.bean.proceso4.Area;
import com.util.Util;
import com.view.proceso4.PrincipalProceso4;
import com.view.proceso6.PrincipalProceso6;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.JFrame;

public class IngresoSistema extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private Area a;

	public IngresoSistema() {
		setTitle("Sistema MAPRO Logística - Logeo");
		setResizable(false);
		initComponents();
		setLocationRelativeTo(null);
		jpbBarra.setVisible(false);
	}

	LoginAction la = new LoginAction();
	private Timer tiempo;
	int cont;

	private void initComponents() {

		lblTitulo = new javax.swing.JLabel();
		jpLogin = new javax.swing.JPanel();
		lblUsuario = new javax.swing.JLabel();
		lblContrasena = new javax.swing.JLabel();
		txtUsuario = new javax.swing.JTextField();
		txtUsuario.setText("hoscanoa");
		txtUsuario.setToolTipText("Ingrese su usuario");
		txtContrasena = new javax.swing.JPasswordField("123");
		txtContrasena.setToolTipText("Ingrese su Contraseña");
		btnIngresar = new javax.swing.JButton();
		btnIngresar.setToolTipText("Acceder al sistema");
		lblImagen = new javax.swing.JLabel();
		btnSalir = new javax.swing.JButton();
		btnSalir.setToolTipText("Desea salir del sistema?");
		jpbBarra = new javax.swing.JProgressBar();
		lblLogo = new javax.swing.JLabel();

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		lblTitulo.setFont(new java.awt.Font("Comic Sans MS", 0, 24));
		lblTitulo.setForeground(new java.awt.Color(255, 0, 0));
		lblTitulo.setText("MAPRO LOGÍSTICA");

		jpLogin.setBorder(javax.swing.BorderFactory
				.createTitledBorder("DATOS DE ACCESO"));

		lblUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagenes/user.png")));
		lblUsuario.setText("USUARIO");

		lblContrasena.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagenes/password.png")));
		lblContrasena.setText("Contraseña");

		btnIngresar.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagenes/aceptar.png")));
		btnIngresar.setText("INGRESAR");
		btnIngresar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnIngresarActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jpLoginLayout = new javax.swing.GroupLayout(
				jpLogin);
		jpLogin.setLayout(jpLoginLayout);
		jpLoginLayout
				.setHorizontalGroup(jpLoginLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jpLoginLayout
										.createSequentialGroup()
										.addGroup(
												jpLoginLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jpLoginLayout
																		.createSequentialGroup()
																		.addGap(24,
																				24,
																				24)
																		.addGroup(
																				jpLoginLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								lblContrasena,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								lblUsuario,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.addGap(18,
																				18,
																				18)
																		.addGroup(
																				jpLoginLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								txtContrasena,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								260,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								txtUsuario,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								260,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																jpLoginLayout
																		.createSequentialGroup()
																		.addGap(405,
																				405,
																				405)
																		.addComponent(
																				btnIngresar,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				138,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		jpLoginLayout
				.setVerticalGroup(jpLoginLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jpLoginLayout
										.createSequentialGroup()
										.addGap(25, 25, 25)
										.addGroup(
												jpLoginLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																lblUsuario)
														.addComponent(
																txtUsuario,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(27, 27, 27)
										.addGroup(
												jpLoginLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																txtContrasena,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblContrasena))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(btnIngresar)
										.addContainerGap()));

		lblImagen.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagenes/login.png")));

		btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagenes/exit3.png")));
		btnSalir.setBorderPainted(false);
		btnSalir.setContentAreaFilled(false);
		btnSalir.setPressedIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/Imagenes/exit1.png")));
		btnSalir.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/Imagenes/exit3.png")));
		btnSalir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSalirActionPerformed(evt);
			}
		});

		lblLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagenes/logo.png")));
		lblLogo.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(10, 10,
																		10)
																.addComponent(
																		btnSalir)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		jpbBarra,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE))
												.addComponent(
														jpLogin,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE))
				.addGroup(
						layout.createSequentialGroup()
								.addGap(24, 24, 24)
								.addComponent(lblLogo)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(lblTitulo).addGap(80, 80, 80)
								.addComponent(lblImagen).addGap(47, 47, 47)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(28, 28,
																		28)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						lblImagen)
																				.addComponent(
																						lblTitulo)))
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		lblLogo,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		85,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jpLogin,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		btnSalir))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(25, 25,
																		25)
																.addComponent(
																		jpbBarra,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		43,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(27, Short.MAX_VALUE)));

		pack();
	}

	// 1
	private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {
		IniciarSesion();
	}

	// 2
	private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
		if (JOptionPane.showConfirmDialog(rootPane,
				"¿Desea Salir del Sistema?", "SALIR DEL SISTEMA",
				JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_NO_OPTION)
			System.exit(0);
	}

	// 3
	public final static int TWO_SECOND = 1;
	class TimerListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent ae) {
			cont++;
			jpbBarra.setValue(cont);
			if (cont == 1) {
				tiempo.stop();
				esconder();

				//String jun = txtUsuario.getText().substring(0, 2);
				if (a.getDescripcion().equals("Almacén")) {
					PrincipalProceso4 p4 = new PrincipalProceso4();
					Util.Mensaje(null, "BIENVENIDO AL SISTEMA DE ALMACÉN",
							Util.CONFIRMACION);
					p4.setVisible(true);
				} else if (a.getDescripcion().equals("Logística")) {
					PrincipalProceso6 p6 = new PrincipalProceso6();
					Util.Mensaje(null, "BIENVENIDO AL SISTEMA DE LOGÍSTICA",
							Util.CONFIRMACION);
					p6.setVisible(true);
				}
			}
		}
	}

	// 4
	public void esconder() {
		this.setVisible(false);
	}

	// 5
	public void activar() {
		tiempo.start();
	}

	// 6
	public void IniciarSesion() {
		String username = txtUsuario.getText();
		@SuppressWarnings("deprecation")
		String password = txtContrasena.getText();
		a = la.Logeo(username, password);
		if (a != null) {
			jpbBarra.setVisible(true);
			cont = -1;
			jpbBarra.setValue(0);
			jpbBarra.setStringPainted(true);
			tiempo = new Timer(TWO_SECOND, new TimerListener());
			activar();
		} else
			Util.Mensaje(null, "Los datos son incorrectos vuelva a intentarlo",
					Util.ERRROR);
	}

	private javax.swing.JButton btnIngresar;
	private javax.swing.JButton btnSalir;
	private javax.swing.JPanel jpLogin;
	private javax.swing.JProgressBar jpbBarra;
	private javax.swing.JLabel lblContrasena;
	private javax.swing.JLabel lblImagen;
	private javax.swing.JLabel lblLogo;
	private javax.swing.JLabel lblTitulo;
	private javax.swing.JLabel lblUsuario;
	private javax.swing.JPasswordField txtContrasena;
	public static javax.swing.JTextField txtUsuario;

}
